package com.eims.sls_app.ui;

import com.eims.sls_app.utils.App;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import bls.juejin.cn.R;

/**
 * 支付webviewActivity
 * 
 */
public class PayWebActivity extends MyBaseActivity implements OnClickListener {

	private View btn_back;
	private WebView webView;

	private StringBuffer strBuffer;
	public static int RESULT_OK = 200;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay_webview_layout);
		getParamter();
		initView();
	}

	@SuppressLint("NewApi")
	private void initView() {
		btn_back = findViewById(R.id.btn_back);
		btn_back.setOnClickListener(this);

		webView = (WebView) findViewById(R.id.webView);
		webView.loadUrl(strBuffer.toString());

		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);

		// User settings

		webSettings.setJavaScriptEnabled(true);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		webSettings.setUseWideViewPort(true);// 关键点

		webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

		webSettings.setDisplayZoomControls(false);
		webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
		webSettings.setAllowFileAccess(true); // 允许访问文件
		webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮
		webSettings.setSupportZoom(true); // 支持缩放

		webSettings.setLoadWithOverviewMode(true);

		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int mDensity = metrics.densityDpi;
		if (mDensity == 240) {
			webSettings.setDefaultZoom(ZoomDensity.FAR);
		} else if (mDensity == 160) {
			webSettings.setDefaultZoom(ZoomDensity.MEDIUM);
		} else if (mDensity == 120) {
			webSettings.setDefaultZoom(ZoomDensity.CLOSE);
		} else if (mDensity == DisplayMetrics.DENSITY_XHIGH) {
			webSettings.setDefaultZoom(ZoomDensity.FAR);
		} else if (mDensity == DisplayMetrics.DENSITY_TV) {
			webSettings.setDefaultZoom(ZoomDensity.FAR);
		} else {
			webSettings.setDefaultZoom(ZoomDensity.MEDIUM);
		}

		/**
		 * 用WebView显示图片，可使用这个参数 设置网页布局类型： 1、LayoutAlgorithm.NARROW_COLUMNS ：
		 * 适应内容大小 2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕，内容将自动缩放
		 */
		webSettings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
		webView.addJavascriptInterface(new JavaScriptObject(this), "mlkxJs");

		webView.setWebChromeClient(new WebChromeClient() {

			@Override
			public boolean onJsAlert(WebView view, String url, String message,
					final JsResult result) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						PayWebActivity.this);
				builder.setMessage(message)
						.setNeutralButton("确定",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {

									}
								}).show();
				result.cancel();
				return true;
			}
		});
	}

	private void getParamter() {
		// strBuffer = new StringBuffer(App.url);
		strBuffer = new StringBuffer();
		strBuffer.append(App.web_pay_url);
		if (App.user != null) {
			strBuffer.append(App.user.getName());
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			backMyCenter();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			backMyCenter();
			break;
		}
	}

	private void backMyCenter() {
		Intent intent = new Intent();
		// 把返回数据存入Intent
		intent.putExtra("result", "My name is linjiqin");
		// 设置返回数据
		this.setResult(RESULT_OK, intent);
		finish();
	}

	public class JavaScriptObject {
		Context mContxt;

		public JavaScriptObject(Context mContxt) {
			this.mContxt = mContxt;
		}

		@JavascriptInterface
		public void finishCurrentPage() {
			finish();
		}

		@JavascriptInterface
		public void shareInfo(String method, String url, String icon,
				String text, String content) {

		}
	}
}
